package com.example.demo.practice.area;


import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ExpandableListView;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.innerclass.NormalExpandableListAdapter;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.ui.PieChartBean;
import com.example.demo.practice.ui.PieChart_View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class AreaStatistic extends IBaseActivity {
    private int etp_number = 0;//设备总数
    private ArrayList<String> type = new ArrayList<>();//设备类型
    private ArrayList<Integer> type_number = new ArrayList<>();//类型数量
    private ArrayList<String > color = new ArrayList<>();

    private ExpandableListView List;
    private List<Integer> horList;
    private List<String> groupData;
    private ArrayList<String> groupname;
    private ArrayList<Integer> groupnum;
    private List<List<String>> childData;
    private PieChart_View pieView;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray = serverHttp.getData();
            groupData = new ArrayList<String>();
            groupname = new ArrayList<>();
            groupnum = new ArrayList<>();
            childData = new LinkedList<List<String>>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    String adName = json.getString("admregion");
                    String name = json.getString("name");
                    if (groupname.contains(adName)) {
                        for (int j = 0; j < groupname.size(); j++) {
                            if (groupname.get(j).equals(adName) && (!childData.get(j).contains(name))) {
                                childData.get(j).add(name);
                                groupnum.set(j,groupnum.get(j) + 1);
                                break;
                            }
                        }
                    } else {
                        groupname.add(adName);
                        groupnum.add(1);
                        List<String> list = new ArrayList<String>();
                        list.add(name);
                        childData.add(list);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            for (int z = 0;z < groupname.size();z++){
                String all = null;
                all = groupname.get(z) + "  (" + groupnum.get(z) + "个)";
                groupData.add(all);
            }
            etp_number = random();
            pieView.setPieName("设备总数：" + etp_number + "个");
            initHorzion(etp_number);
            NormalExpandableListAdapter normalExpandableListAdapter = new NormalExpandableListAdapter(groupData, childData);
            List.setAdapter(normalExpandableListAdapter);
            List.setOnChildClickListener(new ChildrenClick());
            serverHttp.clear();
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arealist);
        pieView = (PieChart_View) findViewById(R.id.pie_view);
        setTitle("区域统计");
        color.add("#fe996b");
        color.add("#00d4bd");
        color.add("#f868ae");
        color.add("#a65ced");
        color.add("#01c6fd");
        color.add("#c076f1");
        color.add("#00d1fc");
        color.add("#00dcfe");
        type.add("疏散灯");
        type.add("火灾显示盘");
        type.add("火灾报警器");
        type.add("应急照明灯");
        type.add("消防栓");
        List = (ExpandableListView) findViewById(R.id.list);
        horList = new ArrayList<>();
        serverHttp = new ServerHttp(handler);
        serverHttp.setUrl("enterprise");
    }
    public class ChildrenClick implements ExpandableListView.OnChildClickListener{

        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
            //String geom = String.valueOf(coordinates.get(i).get(i1));
            etp_number = random();
            pieView.setPieName("设备总数：" + etp_number + "个");
            initHorzion(etp_number);
            return false;
        }
    }

    public int random(){
        Random rand = new Random();
        int ind = rand.nextInt(100);
        return ind > 4 ? ind:random();
    }//生成大于4的随机数

    private void initHorzion(int num){
        horList.clear();
        int ave = num/(type.size()-1);
        for (int i=0;i<type.size()-1;i++){
            horList.add(ave);
        }
        horList.add(num - (type.size()-1)*ave);
        ArrayList<PieChartBean> lists = new ArrayList<>();
        lists.clear();

        for (int nu = 0;nu < type.size();nu ++){
            lists.add(new PieChartBean(Color.parseColor(color.get(nu)), horList.get(nu), type.get(nu)+horList.get(nu)+"个"));
        }
        pieView.setData(lists);

    }
}
